package com.idp.web.test.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.test.entity.PutStatus;
import com.idp.web.test.service.PutStatusService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * 123controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/putStatus")
public class PutStatusController extends BaseController {

	private Logger logger = Logger.getLogger(PutStatusController.class);

	@Resource
	private PutStatusService putStatusService;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init() {

		return "test/putStatusSearch";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	分页查询
	 * </pre>
	 * 
	 * @param putStatus
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(PutStatus putStatus, Page<PutStatus> page, HttpServletRequest request) {

		try {
			request.setAttribute("page", putStatusService.findByPage(putStatus, page));
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}
		return "test/putStatusList";
	}

	/**
	 * 列出所有的入库状态
	 * 
	 * @return
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public String listAll() {
		JSONObject jsonObject = new JSONObject();
		List<PutStatus> putStatus = putStatusService.findBySearch(null);
		if (putStatus != null) {
			jsonObject.put("result", "success");
			jsonObject.put("status", putStatus);
		} else {
			jsonObject.put("result", "faile");
		}
		return jsonObject.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/putStatus")
	public String putStatus(Integer id, HttpServletRequest request) {

		try {

			if (ValidateUtils.isNotEmpty(id)) {
				PutStatus putStatus = putStatusService.getById(id);
				request.setAttribute("putStatus", putStatus);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "test/putStatus";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param putStatus
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(PutStatus putStatus) {

		JSONObject json = new JSONObject();

		try {

			// 修改
			if (ValidateUtils.isNotEmpty(putStatus.getId())) {

				putStatusService.update(putStatus);
			}
			// 新增
			else {

				putStatusService.add(putStatus);
			}
			json.put("result", "save_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}

		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {

		JSONObject json = new JSONObject();

		try {

			putStatusService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}

		return json.toString();
	}
}
